package com.sr.controller.api;

import com.sr.annotation.ApiAuth;
import com.sr.constant.Constant;
import com.sr.controller.BaseController;
import com.sr.ibase.IBaseService;
import com.sr.pojo.WSign;
import com.sr.service.ISignService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.SignReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author xiaochi
 * @date 2022/5/1 11:04
 * @desc SignControllerApi
 */
@Api(tags = "签到模块")
@RestController
@RequestMapping(Constant.Api.WEB_PREFIX+"/sign")
@RequiredArgsConstructor(onConstructor = @__)
public class SignControllerApi extends BaseController {

    private final ISignService signService;

    /**
     * 分页查询签到
     * @param reqVo
     * @return
     */
    @ApiOperation(value = "分页查询签到",notes = "分页查询签到")
    @ApiAuth
    @PostMapping("/list/page")
    public R<PageVo<WSign>> pageList(@RequestBody SignReqVo reqVo){
        IBaseService.Criteria<WSign> criteria = signService.createCriteria(WSign.class);
        criteria.orEqualTo(WSign::getMemberId,this.getUserId());
        return R.ok(signService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    /**
     * 新增签到
     * @param reqVo
     * @return
     */
    @ApiOperation(value = "新增签到",notes = "新增签到")
    @ApiAuth
    @PostMapping("/create")
    public R<String> create(@RequestBody @Valid SignReqVo reqVo){
        WSign wSign = reqVo.buildEntity();
        wSign.setMemberId(this.getUserId());
        return this.response(signService.insert(wSign));
    }
}
